import {
  VuexModule,
  Module,
  Action,
  Mutation,
  getModule
} from 'vuex-module-decorators'
import { getToken, setToken, removeToken } from '@/utils/cookies'
import router, { resetRouter } from '@/router'
import { PermissionModule } from './permission'
import { TagsViewModule } from './tags-view'
import store from '@/store'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import md5 from 'js-md5'
import {
  loginUser,
  loginSocialUser,
  getUserInfo,
  getUserMenuTree
} from '@/api/user'
import vm from '@/main'
import { UserInfo } from '@/models/users'
import db from '@/utils/db'

export interface IUserState {
  token: string
  userInfo: any
  menus: any[]
  permissions: string[]
}

@Module({ dynamic: true, store, name: 'user' })
class User extends VuexModule implements IUserState {
  public token = getToken() || ''
  public userInfo: any = {}
  public menus: any[] = []
  public permissions: string[] = []

  @Mutation
  private SET_TOKEN(token: string) {
    this.token = token
  }

  @Mutation
  private SET_USER_INFO(info: any) {
    console.log('2313====', info)
    this.userInfo = info
  }

  @Mutation
  private SET_MENUS(menus: any[]) {
    this.menus = menus
  }

  @Mutation
  private SET_PERMISSIONS(permissions: string[]) {
    this.permissions = permissions
  }

  @Action
  public async Login(userInfo: { username: string, password: string }) {
    let { username, password } = userInfo
    username = username.trim()
    const data: any = await loginUser({
      phoneNum: username,
      password: md5(password)
    })
    console.log(data.data.sid)
    if (data.data) {
      // this.userInfo = data.data
      this.SET_USER_INFO(data.data)
      localStorage.setItem('sid', data.data.sid)
      // localStorage.setItem('sid', data.data.sid)
      // db.set('userInfo', data.data)
      vm.$router.push('/')
    }
  }

  @Action
  public async SocialLogin(loginParams: {
    socialType: string
    openId: string | undefined
    appToken: string
    userToken: string
  }) {
    const data: any = await loginSocialUser(loginParams)
    if (data.access_token) {
      setToken(data.access_token)
      this.SET_TOKEN(data.access_token)
    }
    console.log(data)
  }

  @Action
  public ResetToken() {
    removeToken()
    this.SET_TOKEN('')
  }

  @Action
  public async GetUserInfo() {
    if (!localStorage.getItem('sid')) {
      this.LogOut()
    }
    const userRes = await getUserInfo()
    // const menuRes = await getUserMenuTree()
    if (userRes.data) {
      this.SET_USER_INFO(userRes.data)
      db.set('userInfo', userRes.data)
      // db.set('menus', menuRes.data)
    }
  }

  @Action
  public async LogOut() {
    this.SET_USER_INFO({})
    // this.ResetToken()
    localStorage.removeItem('sid')
    db.clear()
    // Reset visited views and cached views
    TagsViewModule.delAllViews()
    PermissionModule.CLEAN_ROUTES()
  }

  @Action
  public async SetUserInfo(info: any) {
    console.log(info, 'action调用')
    this.SET_USER_INFO(info)
  }
}

export const UserModule = getModule(User)
